package com.water.fish.anim

import android.graphics.Camera
import android.view.animation.Animation
import android.view.animation.Transformation

/**
 *  type:0向右边镜像,1向左边镜像.
 *  Created by zxn on 2021/10/15.
 **/
class MirrorRotationAnim(private val type: Int = 0) : Animation() {

    private var centerX = 0F

    private var centerY = 0F

    private val camera: Camera = Camera()

    /**
     * 获取坐标
     */
    override fun initialize(width: Int, height: Int, parentWidth: Int, parentHeight: Int) {
        super.initialize(width, height, parentWidth, parentHeight)
        //获得中心点坐标
        centerX = width / 2F
        centerY = width / 2F
        duration = 500
        //repeatMode =
        fillAfter = true
    }

    /**
     * 旋转的角度设置
     */
    override fun applyTransformation(interpolatedTime: Float, t: Transformation) {
        val matrix = t.matrix
        camera.save();
        //设置camera的位置
        camera.setLocation(0F, 0f, 180F)
        //中心是Y轴旋转，这里可以自行设置X轴 Y轴 Z轴
        //camera.rotateY(360 * interpolatedTime)
        if (type == 1) {
            camera.rotateY(-180F)
        } else {
            camera.rotateY(180F)
        }
        //把我们的摄像头加在变换矩阵上
        camera.getMatrix(matrix)
        //设置翻转中心点
        matrix.preTranslate(-centerX, -centerY)
        matrix.postTranslate(centerX, centerY)
        camera.restore()
    }

}